package com.liuyutao.Proxy;

import com.liuyutao.User.UserDao;
import com.liuyutao.aspect.MyAspect;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class MyProxy implements InvocationHandler {
    private UserDao userDao;
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
    public Object getProxy(){
        return Proxy.newProxyInstance(this.getClass().getClassLoader(), userDao.getClass().getInterfaces(), (java.lang.reflect.InvocationHandler) this);
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args)throws Throwable{
        MyAspect myAspect = new MyAspect();
        myAspect.checkPermissions();
        Object obj = method.invoke(userDao,args);
        myAspect.log();
        return obj;
    }

}
